<!---
	Title:		Step 5: Daily Archive Cleanup (Daily Scheduled run)
	Author:		Scott Johnston
	Purpose:	This page cleans out the Archived_Users container of accounts archived 7 or more days.
				Also, cleans up the Home Folder Archives for archives 90 days or older.
	Version:	0.0 Initial Creation 12/28/2009, STJ
	History:	

	

	*** TECHNICAL OUTLINE ***
			
	1)  Clean out archived network accounts after 7 days
	2)  Clean out archived home folders after 3 months
				
--->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Daily Archive Cleanup</title>
</head>

<!--- <cfinclude template="../authentication.cfm">  ---> 
<cfset BASE = CreateObject("component", "#session.rootDir#.cfc.base") />

<cfif (Session.bolAuth EQ True and (Session.strAuthType EQ Session.IT OR Session.strAuthType EQ Session.HR OR Session.strUserID EQ Session.SERVICE_ACCOUNT))>	
	<cfobject type="dotnet" name="objTerm" class="ADTools.Termination" assembly ="C:\Program Files\Southcentral Foundation\ADTools\ADTools.dll">
    
	<!---Retention Constants, in days--->
    <cfset ACCT_RETENTION = 7> 
    <cfset FOLDER_RETENTION = 90> 
    <cfset CLEANUP_STEP = 26>  
    <cfset FOLDERS_TO_EXCLUDE = "'DELETEDITEMSEXMERGE'"> <!--- This can be one or many folder names.  To add more folders to exclude, add comma delimeted items in qoutes (').  Ex: "'Test1', 'Test2', 'Test3'" --->
    <cfset ARCHIVE_DIR = "\\scfappshare\ITS_Data\Archive"> 

    <cfset strError = "">
    <cfset strSummary = "">
    
	<!--- Clean out archived network accounts --->
    <cfquery name="qryGetAcctCleanupWaiting" datasource="#Session.DataSource#">
        scf_GetWaiting 26, 1, 1
    </cfquery>	
    <cfquery name="qryGetExpiredAcctArchives" dbtype="query">            
        Select *
        From qryGetAcctCleanupWaiting
        Where CreationTimeStamp <= <cfqueryparam cfsqltype="cf_sql_timestamp" value="#DateAdd('d',(-1)*ACCT_RETENTION,Now())#">
    </cfquery>	
        
       	
	<cfset strSummary = 'Scheduled Account Archive Cleanup.  <BR/>Started:  #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR/><BR/>'  >	
    <cfloop query = "qryGetExpiredAcctArchives">
        <cftry>
            <cfset intTermReqID = qryGetExpiredAcctArchives.TermReqID>
            <cfset lngResult = 0>	
            <cfset strResult = "">
            
            <cfquery name="qryGetTermDetails" datasource="#Session.DataSource#">
                scf_GetTermDetails #intTermReqID#, 26
            </cfquery>
            <cfset intStepID = qryGetTermDetails.ActionStepID>
            <cfset strUserID = qryGetTermDetails.UserAccount> 
            <cfset strGUID= qryGetTermDetails.AD_GUID>
            
            <cfset strSummary &= 'Removing User:  #strUserID#    ----    '>
            <!---cfset lngResult = objTerm.DeleteAccount("#strUserID#")---> 
            <cfset lngResult = objTerm.DeleteAccount("#strGUID#")><!--- The UserID works fine in most cases, but for those with an alias where the alias is the primary SMTP address/mailbox, it fails.  Seems to work pretty well if the GUID is passed.--->
                
            <cfif lngResult EQ 0>
                <cfset strSummary &= 'Success<BR/>'>
                <cfquery name="qryUpdateAction" datasource="#Session.DataSource#">
                    scf_UpdateAction #intStepID#, -19, 'Scheduled Execution'
                </cfquery>
            <cfelse>
                <cfset strResult = objTerm.GetErrorText()>
                <cfset strSummary &= '<strong>#strResult#</strong><BR/>'>
                <cfthrow detail="#strResult#">
            </cfif>
            
                              
            <cfcatch type="any">
                <cfset strError &= ' Message: #CFCATCH.message#<BR /> 
                    Detail: #CFCATCH.Detail#'>
                <cfquery name="qryInsertErrorLog" datasource="#Session.DataSource#">
                    scf_InsertErrorLog #intStepID#, -19, N'Error in Scheduled User Archive Cleanup: #strError#'
                </cfquery>
                
                <cfset strSummary &= 'Error:  #strError#<BR/>
                    Step: #intStepID#<BR />'>
                    
                <cfmail to=#Session.ITS_EMAIL# From="EPAR_Cleanup_Error@scf.cc" subject="EPAR Scheduled User Archive Cleanup Error" type="html">
                    Error in Scheduled User Archive Cleanup<BR /> 
                    Error Text: #strError#<BR /> 
                    Step: #intStepID#<BR />
                    Action by User: Service account (scheduled)<BR /><BR />
                    
                    Addition Info:<BR />
                    #strSummary#
                </cfmail>
            </cfcatch>
    
        </cftry>
        
    </cfloop>
    <cfset strSummary &= '<BR/>Account Archive Cleanup Completion:  #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR /><BR /><BR/>'  >	
    
    
    
    <cfset strSummary &= 'Scheduled Home Folder Archive Cleanup.  <BR/>Started:  #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR/><BR/>'  >	
    <cfdirectory action="list" directory="#ARCHIVE_DIR#" recurse="no" name="objContents" >
    
    <cfquery name="ArchivesToDelete" dbtype="query">
        Select * from objContents
        Where DateLastModified <= #DateAdd('d',(-1)*FOLDER_RETENTION,Now())# AND Name not in (#replace(FOLDERS_TO_EXCLUDE,"''","'", "all")#)
        Order by Name
    </cfquery>    
    
    <cfscript>
        objFSO = CreateObject("COM", "Scripting.FileSystemObject");
    </cfscript>
    
    <cfloop query="ArchivesToDelete">
    	<cfset strFolder = "">
        <cftry>
			<cfif ArchivesToDelete.Type EQ "Dir"> 
                <cfset strFolder = ARCHIVE_DIR & "\" & ArchivesToDelete.Name>
                <cfset strDateMod = "#dateFormat(ArchivesToDelete.DateLastModified, 'mm/dd/yyyy')# #TimeFormat(ArchivesToDelete.DateLastModified, 'hh:mm:ss tt')#">
                <cfscript>
                    objFolder = objFSO.GetFolder("#strFolder#");
                </cfscript>
                <!--- If both DateCreated and DateLastModified attributes precede the retention cutoff date, delete the directory --->
                <cfif objFolder.DateCreated LTE #DateAdd('d',(-1)*FOLDER_RETENTION,Now())# AND ArchivesToDelete.DateLastModified LTE #DateAdd('d',(-1)*FOLDER_RETENTION,Now())#>
                	<cfset strDateCreated = "#dateFormat(objFolder.DateCreated, 'mm/dd/yyyy')# #TimeFormat(objFolder.DateCreated, 'hh:mm:ss tt')#">
                	<cfset strSummary &= "<pre>#strFolder# 	Last Mod: #strDateMod# 	Created: #strDateCreated#	Deleted: #dateFormat(Now(), 'mm/dd/yyyy')#">
                    
                    <cfdirectory action="delete" directory="#strFolder#" recurse="yes">
                    <cfquery name="qryLogRemoval" datasource="#Session.DataSource#">
                        scf_InsertArchiveRemovalLog '#strFolder#', '#strDateMod#', '#strDateCreated#'
                    </cfquery>
                    <cfset strSummary &= "	<strong>Success</strong></pre>">
                </cfif>
                
            </cfif>
    
            <cfcatch type="any">
                <cfset strError = ' Message: #CFCATCH.message#<BR /> 
                    Detail: #CFCATCH.Detail#'>
                                
                <cfset strSummary &= '<BR/>Error:  #strError#<BR/>'>
                    
                <cfmail to=#Session.ITS_EMAIL# From="EPAR_Cleanup_Error@scf.cc" subject="EPAR Scheduled Home Folder Cleanup Error" type="html">
                    Error in Scheduled Home Folder Cleanup<BR /> 
                    Error Text: #strError#<BR /> 
                    Action by User: Service account (scheduled)<BR /><BR />
                    
                    Home Folder: #strFolder#
                </cfmail>
            </cfcatch>
    
        </cftry>
    </cfloop>
    <cfset strSummary &= '<BR/>Home Folder Archive Cleanup Completion:  #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR /><BR />'  >	
       
    
    <cfmail to=#Session.ITS_EMAIL# From="EPAR_Cleanup@scf.cc" subject="EPAR Scheduled Cleanup Summary" type="html">           
        #strSummary#
    </cfmail>
    
    <body>
    
		<cfoutput>
            #strSummary#
        </cfoutput>    
    
    </body>

<cfelse> <!--- Authorization Check --->
	<cfinclude template="../Denied.cfm">
	
</cfif> 

</html>